package com.ita.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ita.pojo.Areas;
import com.ita.pojo.Data;
import com.ita.service.AreasService;
import com.ita.service.impl.AreasServiceImpl;
import com.ita.util.JsonUtil;
import com.ita.util.RequestPayloadUtil;

public class GetAreasListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AreasService areasService;

	public GetAreasListServlet() {
		areasService = new AreasServiceImpl();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		String payload = RequestPayloadUtil.getRequestBody(request);
		ObjectMapper mapper = new ObjectMapper();
		Data data = mapper.readValue(payload, Data.class);
		String keyWord = data.getWords();
		String row = data.getRow();
		if (keyWord.equals("")) {
			response.getWriter().write("");
			return;
		}

		List<Areas> list = areasService.findByKey(keyWord, row);
		String json = JsonUtil.getJsonFromObject(list);
		System.out.println(json);
		response.getWriter().write(json);

	}

}
